package cn.dxj1016.lesson01;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试ip
public class TestInetAddress {
    public static void main(String[] args) throws UnknownHostException {

        try {
//            查询本机地址
            InetAddress byName = InetAddress.getByName("127.0.0.1");
            System.out.println(byName);
            InetAddress localhost = InetAddress.getByName("localhost");
            System.out.println(localhost);
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);

            //查询网站ip地址
            InetAddress byName1 = InetAddress.getByName("www.baidu.com");
            System.out.println(byName1);

//            常用方法
            System.out.println(byName1.getAddress());
            System.out.println(byName1.getCanonicalHostName());//规范的名字
            System.out.println(byName1.getHostAddress());//ip
            System.out.println(byName1.getHostName());//域名，或者自己计算机的名字

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
/*
执行结果：
/127.0.0.1
localhost/127.0.0.1
XRSTSXUZU819DU7/192.168.1.9
www.baidu.com/14.215.177.38
[B@1b6d3586
14.215.177.38
14.215.177.38
www.baidu.com
 */